package org.cying.main.controller;

import cn.hutool.http.HttpStatus;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.cying.main.domain.Department;
import org.cying.main.domain.SampleResult;
import org.cying.main.feign.DeptFeignSign;
import org.cying.main.feign.DeptFeignStudent;
import org.cying.main.feign.DeptFeignTask;
import org.cying.main.handler.GlobalFlowHandler;
import org.cying.main.service.DeptService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Department Server 控制类
 * @author Cying
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping(value = "/dept")
public class DepartmentController {

    @Resource
    private DeptService service;

    @Resource
    private DeptFeignStudent sSer;

    @Resource
    private DeptFeignTask tSer;

    @Resource
    private DeptFeignSign uSer;


    /**
     * 修改部门名
     * Step1、创建新部门
     * Step2、修改数据库中的 {department, group} 映射表
     * Step3、删除旧部门
     * @param oldName 旧名
     * @param newName 新名
     * @return 更新结果
     */
    @PostMapping(value = "/alter/department")
    @SentinelResource(
            value = "alter-department",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHand_ss"
    )
    public SampleResult<Object> alterDepartment(
            @RequestParam("oldName") String oldName,
            @RequestParam("newName") String newName
    ) {
        int cd = service.createDept(newName);
        int ad = service.alterDept(oldName, newName);
        boolean dd = service.deleteDept(oldName);
        return (cd == 1 && ad > 0 && dd)
                ? new SampleResult<>("/dept/alter/department", HttpStatus.HTTP_OK, HttpStatus.HTTP_OK,  "修改成功")
                : new SampleResult<>("/dept/alter/department", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "修改失败");
    }

    /**
     * 修改组别名
     * Step1、创建新小组
     * Step2、修改数据库中的 {department, group} 映射表
     * Step3、删除旧小组
     * @param oldName 旧名
     * @param newName 新名
     * @return 更新结果
     */
    @PostMapping(value = "/alter/group")
    @SentinelResource(
            value = "alter-group",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHand_ss"
    )
    public SampleResult<Boolean> alterGroup(
            @RequestParam(value = "department", required = true) String department,
            @RequestParam(value = "oldName", required = true) String oldName,
            @RequestParam(value = "newName", required = true) String newName
    ) {
        boolean cg = service.createGroup(department, newName);
        boolean ag = service.alterGroup(oldName, newName);
//        boolean dg = service.deleteGroup(department, oldName);
        boolean dg = true;
        System.out.println(cg + ", " + ag + ", " + dg);
        return (cg && ag && dg)
                ? new SampleResult<>("/dept/alter/group", HttpStatus.HTTP_OK, true,  "修改成功")
                : new SampleResult<>("/dept/alter/group", HttpStatus.HTTP_NOT_ACCEPTABLE, false, "修改失败");
    }
    
    /**
     * 创建新的部门
     * Step1、创建新部门
     * Step2、为部门创建 "部长"、"副部长"
     * @param name 部门名
     * @return 创建结果
     */
    @PostMapping(value = "/create/department")
    @SentinelResource(
            value = "create-department",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHand_s"
    )
    public SampleResult<Object> createDepartment(
            @RequestParam(value = "name") String name
    ) {
        int cd = service.createDept(name); // 创建部门
        int ci = service.createInit(name); // 创建部长与副部长职位
        return (cd == 1 && ci == 2)
            ? new SampleResult<>("/dept/create/department", HttpStatus.HTTP_OK, null, name + "创建成功")
            : new SampleResult<>("/dept/create/department", HttpStatus.HTTP_NOT_ACCEPTABLE, null, name + "创建失败");
    }
    
    /**
     * 创建新的小组
     * @param department 部门名
     * @param group 组别名
     * @return 创建结果
     */
    @PostMapping(value = "/create/group")
    @SentinelResource(
            value = "create-group",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHand_s"
    )
    public SampleResult<Object> createGroup(
            @RequestParam(value = "department", required = true) String department,
            @RequestParam(value = "group", required = true) String group
    ) {
        return service.createGroup(department, group)
            ? new SampleResult<>("/dept/create/group", HttpStatus.HTTP_OK, null, group + "创建成功")
            : new SampleResult<>("/dept/create/group", HttpStatus.HTTP_NOT_ACCEPTABLE, null, group + "创建失败");
    }

    /**
     * 删除指定名称的部门
     * @param name 部门名
     * @return 删除结果
     */
    @DeleteMapping(value = "/delete/dept")
    @SentinelResource(
            value = "delete-department",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHand_s"
    )
    public SampleResult<Object> deleteDept(
            @RequestParam("name") String name
    ) {
        uSer.deleteRecord(name, null);
        tSer.deleteTask(null, name, null, null);
        sSer.deleteRecord(null, name, null);
        return service.deleteDept(name)
                ? new SampleResult<>("/dept/delete/department", HttpStatus.HTTP_OK, null,  "删除成功")
                : new SampleResult<>("/dept/delete/department", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "删除失败");
    }

    /**
     * 删除指定名称的小组
     * @param name 小组名
     * @return 删除结果
     */
    @DeleteMapping(value = "/delete/group")
    @SentinelResource(
            value = "delete-group",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHand_s"
    )
    public SampleResult<Object> deleteGroup(
            @RequestParam(value = "department", required = true) String department,
            @RequestParam(value = "group", required = true) String group
    ) {
        uSer.deleteRecord(department, group);
        tSer.deleteTask(null, department, group, null);
        sSer.deleteRecord(null, department, group);
        return service.deleteGroup(department, group)
                ? new SampleResult<>("/dept/delete/group", HttpStatus.HTTP_OK, null,  "删除成功")
                : new SampleResult<>("/dept/delete/group", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "删除失败");
    }

    /**
     * 从数据库获取 {部门, 小组} 映射到 List 中再返回
     * @param tid 任务id
     * @return 查询结果
     */
    @GetMapping(value = "/get/department")
    @SentinelResource(
            value = "get-department",
            blockHandlerClass = GlobalFlowHandler.class,
            blockHandler = "flowHandler_s"
    )
    public SampleResult<List<Department>> getDepartment(
            @RequestParam(value = "tid", required = false) String tid
    ) {
        System.out.println(tid);
        List<Department> result = service.getAllDept(tid);
        return result.isEmpty()
                ? new SampleResult<>("/dept/get/department", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "部门信息获取失败")
                : new SampleResult<>("/dept/get/department", HttpStatus.HTTP_OK, result);
    }

    @PostMapping(value = "/move/group")
    public SampleResult<Object> moveGroup(
            @RequestParam(value = "oldDepartment") String oldDepartment,
            @RequestParam(value = "newDepartment") String newDepartment,
            @RequestParam(value = "group") String group
    ) {
        return service.moveGroup(oldDepartment, newDepartment, group)
                ? new SampleResult<>("/dept/move/group", HttpStatus.HTTP_OK, null, "移动成功")
                : new SampleResult<>("/dept/move/group", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "移动失败");
    }

}